package dao;

import entity.Category;
import entity.Photographer;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;

import java.util.List;

/**
 * Created by jifeng on 2016/12/12 0012.
 */
public class CategoryDao {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    private Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    public void addCategory(Category category){

        getCurrentSession().save(category);
    }
    public List findCategoryList(){
        List<Category> categoryList = null;
        String sql = "select * from Category";
        categoryList= getCurrentSession().createSQLQuery(sql).addEntity(Category.class).list();
        return categoryList;
    }

    public Category findCategoryById(int id){
        String sql="SELECT * FROM category c WHERE c.id="+id+"";
        List<Category> categoryList = getCurrentSession().createSQLQuery(sql).list();
        return categoryList.get(0);
    }

    public void deleteCategory(int id){
        getCurrentSession().createQuery("delete from category c where c.id ="+id+"").executeUpdate();
    }

    public void updateCate(int id,String name){
        String sql="UPDATE category c SET c.categoryName ='"+name+"' WHERE c.id="+id+"";
        getCurrentSession().createSQLQuery(sql).executeUpdate();
    }
    
}
